"""供api返回的全局状态码"""

import json


# 自定义错误码
def ERROR_CODE_CUSTOMIZE(message: list | str, code: int = 500):
    """自定义错误码，使用时需要传入message和code"""
    return (
        json.dumps(
            {
                "message": message,
                "status_code": "ERROR_CODE_CUSTOMIZE",
                "status": "error",
            },
            ensure_ascii=False,
        ),
        code,
    )


# 自定义正确码
def SUCCESS_CODE_CUSTOMIZE(message: list | str, code=200):
    """自定义成功码，使用时需要传入message和code"""
    return (
        json.dumps(
            {
                "message": message,
                "status_code": "SUCCESS_CODE_CUSTOMIZE",
                "status": "success",
            },
            ensure_ascii=False,
        ),
        code,
    )


class ERROR_CODE:
    """自定义错误码"""

    @classmethod
    def TOKEN_INVALID(cls):
        return (
            json.dumps(
                {
                    "message": {
                        "refresh_token": {"Value error, refresh_token cannot be empty"}
                    },
                    "status": "error",
                    "status_code": "TOKEN_INVALID",
                },
                ensure_ascii=False,
            ),
            401,
        )

    @classmethod
    def LOG_FILE_NOT_EXISTS(cls):
        return (
            json.dumps(
                {
                    "message": {'日志文件不存在！'},
                    "status": "error",
                    "status_code": "TOKEN_INVALID",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def PROXY_URL_NOT_FOUND(cls, result='代理地址不存在！'):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "error",
                    "status_code": "TOKEN_INVALID",
                },
                ensure_ascii=False,
            ),
            407,
        )

    @classmethod
    def PROXY_REQUEST_ERROR(cls, result='代理请求失败！'):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "error",
                    "status_code": "TOKEN_INVALID",
                },
                ensure_ascii=False,
            ),
            406,
        )

    @classmethod
    def NODE_NOT_FOUND(cls):
        return (
            json.dumps(
                {
                    "message": '节点不存在！',
                    "status": "error",
                    "status_code": "NODE_NOT_FOUND",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def METHOD_NOT_ALLOWED(cls):
        return (
            json.dumps(
                {
                    "message": "请求类型错误",
                    "status": "error",
                    "status_code": "METHOD_NOT_ALLOWED",
                },
                ensure_ascii=False,
            ),
            401,
        )

    @classmethod
    def TOKEN_MISMATCH(cls):
        return (
            json.dumps(
                {
                    "message": "token不匹配",
                    "status": "error",
                    "status_code": "TOKEN_MISMATCH",
                },
                ensure_ascii=False,
            ),
            422,
        )

    @classmethod
    def TOKEN_REVOKED(cls):
        return (
            json.dumps(
                {
                    "message": "token 已撤销",
                    "status": "error",
                    "status_code": "TOKEN_REVOKED",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def UPDATE_FAILED(cls):
        return (
            json.dumps(
                {
                    "message": "更新失败",
                    "status": "error",
                    "status_code": "METHOD_NOT_ALLOWED",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def NO_AUTHORIZATION(cls):
        return (
            json.dumps(
                {
                    "message": "缺少Token",
                    "status": "error",
                    "status_code": "NO_AUTHORIZATION",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def EXPIRED_TOKEN(cls):
        return (
            json.dumps(
                {
                    "message": "Token已过期",
                    "status": "error",
                    "status_code": "EXPIRED_TOKEN",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def NODE_REGISTERED(cls):
        return (
            json.dumps(
                {
                    "message": "节点已注册",
                    "status": "error",
                    "status_code": "NODE_REGISTERED",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def STYPE_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "应用类型错误",
                    "status": "error",
                    "status_code": "STYPE_ERROR",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def STYPE_NOT_CAPACITY(cls):
        return (
            json.dumps(
                {
                    "message": "当前应用类型不具备该能力",
                    "status": "error",
                    "status_code": "STYPE_NOT_CAPACITY",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def INVALID_JSON(cls):
        return (
            json.dumps(
                {
                    "message": "无效的JSON数据",
                    "status": "error",
                    "status_code": "INVALID_JSON",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def NO_JSON_AVAILABLE(cls):
        return (
            json.dumps(
                {
                    "message": "请求中没有JSON数据",
                    "status": "error",
                    "status_code": "NO_JSON_AVAILABLE",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def UNAUTHORIZED(cls):
        return (
            json.dumps(
                {
                    "message": "未经授权",
                    "status": "error",
                    "status_code": "UNAUTHORIZED",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def INTERNAL_SERVER_ERROR(cls, e: str = None):
        if type(e) is str:
            e = "：" + e
        return (
            json.dumps(
                {
                    "message": f"服务器内部错误{e}, ensure_ascii=False",
                    "status": "error",
                    "status_code": "INTERNAL_SERVER_ERROR",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def SERVICE_UNAVAILABLE(cls, msg="服务不可用", code=500):
        return (
            json.dumps(
                {
                    "message": msg,
                    "status": "error",
                    "status_code": "SERVICE_UNAVAILABLE",
                },
                ensure_ascii=False,
            ),
            code,
        )

    @classmethod
    def VERIFICATION_FAILDED(cls, e="数据校验失败"):
        """数据校验失败"""
        return (
            json.dumps(
                {
                    "message": e,
                    "status": "error",
                    "status_code": "VERIFICATION_FAILDED",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def NOT_IMPLEMENTED(cls):
        return (
            json.dumps(
                {
                    "message": "未实现",
                    "status": "error",
                    "status_code": "NOT_IMPLEMENTED",
                },
                ensure_ascii=False,
            ),
            501,
        )

    @classmethod
    def USERNAME_PASSWORD_INCORRECT(cls):
        return (
            json.dumps(
                {
                    "message": "用户名或密码错误",
                    "status": "error",
                    "status_code": "USERNAME_PASSWORD_INCORRECT",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def SIGNUP_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "注册失败",
                    "status": "error",
                    "status_code": "SIGNUP_ERROR",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def USER_NOT_FOUND(cls):
        return (
            json.dumps(
                {
                    "message": "用户不存在",
                    "status": "error",
                    "status_code": "USER_NOT_FOUND",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def USER_ALREADY_EXISTS(cls):
        return (
            json.dumps(
                {
                    "message": "用户已存在",
                    "status": "error",
                    "status_code": "USER_ALREADY_EXISTS",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def INVALID_CREDENTIALS(cls):
        return (
            json.dumps(
                {
                    "message": "无效的凭据",
                    "status": "error",
                    "status_code": "INVALID_CREDENTIALS",
                },
                ensure_ascii=False,
            ),
            401,
        )

    @classmethod
    def DATABASE_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "数据库错误",
                    "status": "error",
                    "status_code": "DATABASE_ERROR",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def SUBMISSION_FAILED(cls):
        return (
            json.dumps(
                {
                    "message": "数据库提交失败！",
                    "status": "error",
                    "status_code": "SUBMISSION_FAILED",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def FILE_NOT_FOUND(cls):
        return (
            json.dumps(
                {
                    "message": "文件未找到",
                    "status": "error",
                    "status_code": "FILE_NOT_FOUND",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def FILE_UPLOAD_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "文件上传失败",
                    "status": "error",
                    "status_code": "FILE_UPLOAD_ERROR",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def FILE_DOWNLOAD_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "文件下载失败",
                    "status": "error",
                    "status_code": "FILE_DOWNLOAD_ERROR",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def INVALID_FILE_TYPE(cls):
        return (
            json.dumps(
                {
                    "message": "无效的文件类型",
                    "status": "error",
                    "status_code": "INVALID_FILE_TYPE",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def ROLLBACK_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "回滚失败",
                    "status": "error",
                    "status_code": "ROLLBACK_ERROR",
                },
                ensure_ascii=False,
            ),
            500,
        )

    @classmethod
    def CONFIGURATION_ERROR(cls):
        return (
            json.dumps(
                {
                    "message": "配置错误",
                    "status": "error",
                    "status_code": "CONFIGURATION_ERROR",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def APP_RUNNING(cls):
        return (
            json.dumps(
                {
                    "message": "应用运行中",
                    "status": "error",
                    "status_code": "APP_RUNNING",
                },
                ensure_ascii=False,
            ),
            400,
        )

    @classmethod
    def APP_STOPED(cls):
        return (
            json.dumps(
                {
                    "message": "应用已停止",
                    "status": "error",
                    "status_code": "APP_STOPED",
                },
                ensure_ascii=False,
            ),
            400,
        )


class SUCCESS_CODE:
    """自定义成功码"""

    @classmethod
    def CERT_INFO(cls, result: dict):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "CERT_INFO",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def CHOOSE_FILE_OK(cls):
        return (
            json.dumps(
                {
                    "message": '选中文件可用！',
                    "status": "success",
                    "status_code": "CHOOSE_FILE_OK",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def LOG_CONTEXT(cls, result: dict):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "LOG_CONTEXT",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def NODES_CERT_OK(cls):
        return (
            json.dumps(
                {
                    "message": '节点证书信息认证成功',
                    "status": "success",
                    "status_code": "NODES_CERT_OK",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def REALTIME(cls, result):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "REALTIME",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def NODE_UNBIND_SUCCESS(cls, result="解绑成功！"):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "NODE_UNBIND_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def APP_REMOVE(cls, result="删除成功"):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "APP_REMOVE",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def NODE_REGISTER_SUCCESS(cls):
        return (
            json.dumps(
                {
                    "message": "注册成功！",
                    "status": "success",
                    "status_code": "NODE_REGISTER_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def LOGIN_SUCCESS(cls, result="登录成功"):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "LOGIN_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def TOKEN_ACTIVE(cls, result="token有效"):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "TOKEN_ACTIVE",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def LOGOUT_SUCCESS(cls, result="退出登录成功"):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "LOGOUT_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def UPDATE_SUCCESS(cls, result="更新成功"):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "LOGIN_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def APP_STATUS_SUCCESS(cls, result="获取成功", sse_id=None):
        return {
            "message": result,
            "status": "success",
            "status_code": "LOGIN_SUCCESS",
            "sse_id": sse_id,
        }

    @classmethod
    def SIGNUP_SUCCESS(cls):
        return (
            json.dumps(
                {
                    "message": "注册成功",
                    "status": "success",
                    "status_code": "SIGNUP_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def DOWNLOAD_FILE(cls, result):
        return (
            json.dumps(
                {
                    "message": result,
                    "status": "success",
                    "status_code": "DOWNLOAD_FILE",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def USER_REMOVED(cls):
        return (
            json.dumps(
                {
                    "message": "用户已删除",
                    "status": "success",
                    "status_code": "USER_REMOVED",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def FILE_UPLOADED(cls):
        return (
            json.dumps(
                {
                    "message": "文件上传成功",
                    "status": "success",
                    "status_code": "FILE_UPLOADED",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def FILE_DOWNLOADED(cls):
        return (
            json.dumps(
                {
                    "message": "文件下载成功",
                    "status": "success",
                    "status_code": "FILE_DOWNLOADED",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def ROLLBACK_SUCCESS(cls):
        return (
            json.dumps(
                {
                    "message": "回滚成功",
                    "status": "success",
                    "status_code": "ROLLBACK_SUCCESS",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def CONFIGURATION_UPDATED(cls):
        return (
            json.dumps(
                {
                    "message": "配置已更新",
                    "status": "success",
                    "status_code": "CONFIGURATION_UPDATED",
                },
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def DATA_FETCHED(cls, result="数据获取成功"):
        return (
            json.dumps(
                {"message": result, "status": "success", "status_code": "DATA_FETCHED"},
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def RUN_SUCCESS(cls, result="启动成功"):
        return (
            json.dumps(
                {"message": result, "status": "success", "status_code": "RUN_SUCCESS"},
                ensure_ascii=False,
            ),
            200,
        )

    @classmethod
    def STOP_SUCCESS(cls, result="停止成功"):
        return (
            json.dumps(
                {"message": result, "status": "success", "status_code": "STOP_SUCCESS"},
                ensure_ascii=False,
            ),
            200,
        )
